#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: zhengpingping

import sys
import xlrd3
import operator

import importlib
import ExcelRead
importlib.reload(sys)

outPath = '../../StarForce/Assets/Scripts/Lua/Data/LuaData/'
#内部使用
search_names = []

#获取表格写入值
def get_write_value(value, typeRow):
    if typeRow == 'int':
        if(not value): value = 0
        value = str(value)
    elif typeRow == 'bool':
        if(not value): value = 0
        value = 'true' if value > 0 else 'false'
    elif typeRow == 'array_int':
        value = ("{@value}").replace("@value", str(value))
    else:
        value = ('"@value"').replace("@value", str(value))
    return value

#获取字段类型名称
def get_type_name(typeRow):
    if typeRow == 'bool':
        return 'boolean'
    elif typeRow == 'int':
        return 'number'
    elif typeRow == 'array_int':
        return 'number[]' 
    else:
        return 'string'

#生成lua代码
def create_item_luaCode(ws):
    field_keys = []
    search_line = ExcelRead.excel_sheet_read(ws)

    string = ('---@class Table_@Name')
    for line in search_line:
        typeRow = get_type_name(line.fieldType)
        tempStr = ('\r\n') + ('---@field public {0} {1} @{2}')
        string += tempStr.format(line.fieldName, typeRow, line.fieldDesc)

    string += ('\r\n') + ('local config = {}')
    string += ('\r\n')
    string += ('\r\n') + ('config.Variable = {')

    for i in range(len(search_line)):
        line = search_line[i]
        tempStr = ('\r\n\t') + ('{0} = {1},')
        string += tempStr.format(line.fieldName, i + 1)

    string += ('\r\n') + ('}')
    string += ('\r\n\n') + ('config.Data = {')
    
    #生成数据
    for i in range(4, ws.nrows):
        tempStr = ('\r\n\t') + ('[@Id] = {@Id')

        for j in range(len(search_line)):
            line = search_line[j]

            cell_value = ExcelRead.get_cell_value(ws, i, line.fieldIdx)
            if(not ExcelRead.valid_check_value(cell_value, line.fieldType)):
                logStr = "{0}=={1}=={2} type is invalid"
                print(logStr.format(line.fieldName, line.fieldIdx, cell_value))
                tempStr = ''; break
            
            cell_value = get_write_value(cell_value, line.fieldType)
            if(j == 0):
                contain = operator.contains(field_keys, cell_value)
                if(contain):
                    print(ws.name + '--表格中key值重复:' + cell_value)
                    tempStr = ''; break
                
                field_keys.append(cell_value)
                tempStr = tempStr.replace("@Id", cell_value)
            else:
                tempStr += ", " + cell_value

        if(tempStr != ''):
            string += tempStr + "},"

    string += ('\r\n') + ('}')
    string += ('\r\n\n') + ('return config')
    string = string.replace("@Name", ws.name)

    search_names.append(ws.name)
    outputName = ("Table_@Name.lua").replace("@Name", ws.name)
    ExcelRead.save_file(outPath + outputName, string)

#生成mgr代码
def create_mgr_luaCode():
    string = ('---@class TableCfg')
    string += ('\r\n') + ('local config = {')
    
    for name in search_names:
        tempStr = ('Table_@Name = "Table_@Name",')
        string += ('\r\n\t') + tempStr.replace("@Name", name)

    string += ('\r\n') + ('}')
    string += ('\r\n')
    string += ('\r\n') + ('return config')

    outputName = outPath + 'TableCfg.lua'
    ExcelRead.save_file(outputName, string)

# 读取excel文件
def excel_sheet_read(file):
    wb = xlrd3.open_workbook(file)
    for sheet in wb.sheets():
        print(sheet.name + ' translate create')
        if(ExcelRead.check_fileName(sheet.name, search_names)):
            create_item_luaCode(sheet)

# 初始化方法
ExcelRead.init(outPath, excel_sheet_read)
create_mgr_luaCode()
print('TableCfg translate finished')